//package com.nomito.minimito.netty;
//
//import com.alibaba.fastjson.JSON;
//import com.nomito.minimito.common.constant.MsgActionEnum;
//import com.nomito.minimito.externalUtils.maqueUtils.SpringUtil;
//import com.nomito.minimito.service.ImGroupfriendService;
//import io.netty.channel.Channel;
//import io.netty.channel.ChannelHandlerContext;
//import io.netty.channel.SimpleChannelInboundHandler;
//import io.netty.channel.group.DefaultChannelGroup;
//import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
//import io.netty.util.concurrent.GlobalEventExecutor;
//import org.springframework.stereotype.Component;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * @author 凌月青缃
// * @date 2021/5/23
// */
//@Component
//public class GroupHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
//
//    //用于记录和管理所有客户端的channel
//    public static DefaultChannelGroup users = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
//
//
//    @Override
//    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
//        //获取客户端所传输的消息
//        String content = msg.text();
//        //1.获取客户端发来的消息
////        System.out.println("收到的消息："+content);
//        DataContent dataContent = JSON.parseObject(content, DataContent.class);
//        Integer action = dataContent.getAction();
//        Channel channel =  ctx.channel();
//
//        if(action == MsgActionEnum.GROUPMSG.type){
//
//
//            //2.5 群聊走这里 聊天类型的消息，把聊天记录保存到数据库，
//
//
//            ChatMsg chatMsg = dataContent.getChatMsg();
//            String msgContent = chatMsg.getMsg();
//            Long senderId = chatMsg.getSenderId();
//            Long GroupId = chatMsg.getReceiverId();
//            //保存消息到数据库，并且标记为未签收
//            ImGroupfriendService groupfriendService = (ImGroupfriendService) SpringUtil.getBean("imGroupfriendService");
//            List<Long> groupFriendsId = groupfriendService.listGroupFriendsId(GroupId);
//            DataContent dataContentMsg = new DataContent();
//
//
////            for (int i = 0; i < groupFriendsId.size(); i++) {
////                Channel receiverChannel = UserChanelRel.get(groupFriendsId.get(i));
////                if (receiverChannel!=null){
////                    Channel findChannel = users.find(receiverChannel.id());
////                    if (findChannel != null){
////                        usersizes++;
////                        onlineUserIds.add(groupFriendsId.get(i));
////                    }else continue;
////                }continue;
////            }
//
//            Integer usersizes = 0;//记录在线用户数的标志位
//            List<Long> onlineUserIds = new ArrayList<>();
//            for (Long gfId : groupFriendsId) {
//                Channel receiverChannel = UserChanelRel.get(gfId);
//                //如果该用户不在线
//                if (receiverChannel!=null){
//                    usersizes++;
//                    onlineUserIds.add(gfId);
//                }else continue;
//            }
//
//
//            Integer finalUsersizes = usersizes;
//
//            groupFriendsId.forEach(gfId->{
//                dataContentMsg.setChatMsg(chatMsg);
//                //发送消息
//                Channel receiverChannel = UserChanelRel.get(gfId);
//
//                if (gfId != senderId) {
//                    //发给其他人的消息
//                    if (receiverChannel==null){
//                        //离线用户
////                        users.writeAndFlush(receiverChannel.remoteAddress() + "下线，当前在线人数：" + users.size() + "\n");
//                    }else {
//                        //当receiverChannel不为空时候，从ChannelGroup去查找对应的channel是否存在
//                        Channel findChannel = users.find(receiverChannel.id());
//                        if (findChannel != null){
//                            //用户在线
//                            receiverChannel.writeAndFlush(
//                                    new TextWebSocketFrame(
//                                            JSON.toJSONString(dataContentMsg)
//                                    )
//                            );
//
////                            receiverChannel.writeAndFlush("当前在线用户共："+finalUsersizes+"分别是："+onlineUserIds);
//
//                        }else {
//                            //离线用户
//                        }
//                    }
//
//
//                } else {
//                    //发给自己的消息
////                    receiverChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString("我：" + msg + "\n")));
//                    receiverChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(onlineUserIds)));
//                }
//
//
//
//
//            });
//
//
//
//
//        }
//
//    }
//
//    @Override
//    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
//        users.add(ctx.channel());
//        //遍历一下数据库，然后告诉它所参与的群聊，它上线了（得给每个客户端都发送
//
//    }
//
//    @Override
//    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
//        String chanelId = ctx.channel().id().asShortText();
//        System.out.println("客户端被移除：channel id 为："+chanelId);
//
//        users.remove(ctx.channel());
//    }
//
//    @Override
//    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//        cause.printStackTrace();
//        //发生了异常后关闭连接，同时从channelgroup移除
//        ctx.channel().close();
//        users.remove(ctx.channel());
//    }
//
//
//
////    //服务端监听到客户端活动
////    @Override
////    public void channelActive(ChannelHandlerContext ctx) throws Exception {
////        Channel channel = ctx.channel();
////    }
////
////    //服务端监听到客户端不活动
////    @Override
////    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
////        super.channelInactive(ctx);
////    }
//}
